<?php 
namespace app\user\controller;

use \think\Session;
use \think\Controller;
use think\Request;
use app\user\traits\models\UserInfoCheck;
use app\common\traits\controller\TraitController;
/**
 * 会员找回密码
 */
class UserForgotPwd extends Controller {
	use UserInfoCheck,TraitController;
	public static $key = 'shsytour.cn';
	//前置操作找回密码不需要前置操作
    /**
     * 组合sign
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    protected static function mixSign($data)
    {
        $data = array_diff_key($data, array_flip(['_url', 'sign', 's', '_token', 'action']));
        ksort($data);

        $str = '';
        foreach ($data as $k => $value) {
            $value = urldecode($value);
            if ($value || $value === '0') {
                $str .= $k . $value;
            }
        }
        $str .= 'key' . self::$key;
        return md5($str);
    }
    /**
     * 校验sign
     * @return [type] [description]
     */
    protected function checkToken($data = null)
    {
        $data = $data ? : $this->request->get();
        
        if (isset($data['sign']) && $data['sign'] === $this->mixSign($data)) {
            return true;
        }
        
        return false;
    }
	/**
	 * [getForgetpwd 界面一]
	 * @return [type] [description]
	 */
	public function getForgetpwd()
	{
		return view('userforgotpwd/forgetpwd');
	}
	/**
	 * [postForgetpwdForm 界面一表单]
	 * @return [type] [description]
	 */
	public function getForgetpwdForm()
	{
		$phone = input('phone');$code=input('imgCode');
		$re = model('Users')->getUserInfo(['mobile'=>$phone]);
		$captcha = new \think\captcha\Captcha(config('captcha'));
		if (empty($re)||!$captcha->check($code)) {
			return $this->error('填入错误');
		}
		$param  = [
				'phone' => $phone,
                'imgcode' =>$code,
            ];
        session('phone',$phone);    
        $sign  = $this->mixSign($param);
        $param = array_merge($param, ['sign' => $sign]);    
        $this->redirect(url('/userforgotpwd/hdcode', $param, true, 'user'));
	}

	/**
	 * [getCheckPhone 检测手机]
	 * @return [type] [description]
	 */
	public function getCheckPhone()
	{
		$phone = input('phone');
		$re = model('Users')->getUserInfo(['mobile'=>$phone]);
		if (empty($re)) {
			return json(0);
		}
		return 1;
	}
	/**
	 * [getCheckCode 检测验证码]
	 * @return [type] [description]
	 */
	public function getCheckCode()
	{
		$code=input('imgCode');
        $captcha = new \think\captcha\Captcha(config('captcha'));
        if (!$captcha->check($code)) {
            return json(0);
        }
        return 1;
	}
	/**
	 * [getHdcodeForm 界面二表单]
	 * @return [type] [description]
	 */
	public function getHdcode()
	{
		if (!$this->checkToken($this->request->param())) {
            return $this->error('参数不合法');
        }
		return view('userforgotpwd/code');
	}
	/**
	 * [postHdcodeForm 界面二表单]
	 * @return [type] [description]
	 */
	public function getHdcodeForm()
	{
		$phone_code = input('get.phone_code');
		//$re = model('PhoneCode','../common\model')->check($phone_code,$session);
		$re = model('PhoneCode')->check($phone_code,session_id());
		if ($re) {
			$param  = [
                'code' =>$phone_code,
            ];
            $sign  = $this->mixSign($param);
            $param = array_merge($param, ['sign' => $sign]);
            $this->redirect(url('/userforgotpwd/resetpwd', $param, true, 'user'));
		}else{
			return $this->error(model('PhoneCode')->getError());
		}

	}
	/**
	 * [getPhoneCode 检测手机验证码]
	 * @return [type] [description]
	 */
	public function getPhoneCode()
	{
		$phone_code = input('phone_code');
		$re = model('PhoneCode')->check($phone_code,session_id());
		if ($re) {
			return 1;
		}else{
			return model('PhoneCode')->getError();
		}
	}
	/**
	 * [postSendCode 发送验证码]
	 * @return [type] [description]
	 */
	public function postSendCode()
	{
		$phone = session('phone');
		$re = model('PhoneCode')->send($phone);
		return $re;
	}
	/**
	 * [getResetpwd 界面三]
	 * @return [type] [description]
	 */
	public function getResetpwd()
	{
		if (!$this->checkToken($this->request->param())) {
            return $this->error('参数不合法',url('/userforgotpwd/forgetpwd','','','user'),'',$wait=5);
        }
		return view('userforgotpwd/resetpwd');
	}
	/**
	 * [getCheckPassword 检验密码复杂度]
	 * @return [type] [description]
	 */
	public function getCheckPassword()
	{
		$password = input('password');
		$re = $this->cheack(['password'=>$password]);
		if ($re) {
			return 1;
		}else{
			return json(0);
		}
	}
	/**
	 * [postResetpwdForm 界面三表单]
	 * @return [type] [description]
	 */
	public function getResetpwdForm()
	{
		$phone = session('phone');
		$password = input('get.password');
		$repassword = input('get.repassword');
		$old_psd=model("Users")->where('mobile',$phone)->value('password');
		if (md5($password)==$old_psd) {
			return $this->success('亲，这就是你的原密码',url('/user/login','','','user'),'',$wait=5);
		}
		$re = $this->cheack(['password'=>$password,'repassword'=>$repassword]);
		if ($re) {
			//若修改密码与原密码一致、没修改任何数据返回0.update与setField。
			$result=model("Users")->where('mobile',$phone)->update(['password'=>md5($password)]);
	            if ($result) {
	                session('phone', null);
	                $b=null;
	               	view('user/login',['b'=>$b])->send();
	        		exit;
				}else{
					return $this->success('亲，这就是你的原密码',url('/user/login','','','user'),'',$wait=5);
				}
		}else{
			$this->error('密码不一致');
		}
	}

}